<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/22
 * Time: 10:48
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\api\model\WeixinUser;
use app\common\lib\Show;
use app\common\lib\Status;

class Login extends Base
{
    /**
     * 登录获取openid
     * @return array|false|\think\response\Json
     */
    public function getUserInfo()
    {
        $code = $this->request->param('code', '', 'trim');

        try {
            $code = $this->mini_app->auth->session($code);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "登录失败");
        }
        if(!empty($code['errcode'])){
            return Show::json_show(Status::status_error,"发生错误");
        }
        $data = [
            "openid" => $code['openid'],
            "type" => 0
        ];
        try {
            $wechat = (new WeixinUser())->getUserByOpenid($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($wechat) {
            return Show::json_show(Status::mini_normal, "ok", $wechat);
        } else {
            return false;
        }
    }
}